<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Log;
use Illuminate\Support\Facades\DB;
class Project extends Model
{
    protected $table = 'xfy_project';
    protected $primaryKey = 'projectId';

    protected $fillable = ['projectname', 'projectId'];
    // public function user(){
    // 	return $this->belongsTo(User::class,'user_id','id');
    // }
    // public function __construct(array $attributes = [])
    // {
    //     $this->init();

    //     parent::__construct($attributes);
    // }

    // protected function init()
    // {
    //     $connection = config('admin.database.connection') ?: config('database.default');

    //     $this->setConnection($connection);

    //     $this->setTable('xfy_project');
    // }
    // public function administrators():BelongsToMany
    // {
    //     Log::info('sss');
    //     return $this->belongsToMany(Project::class,ProjectUsers::class,'projectId','user_id')->withTimestamps();
    // }

//     protected static function boot()
//     {
//         parent::boot();
// // Log::info('5555');
//         static::deleting(function ($model) {
//             $model->administrators()->detach();

//             // $model->permissions()->detach();
//         });
//     }
    // public function projects():BelongsToMany
    // {
    //     Log::info('lllll');
    //     dd($this->belongsToMany(Project::class,ProjectUsers::class,'user_id','projectId')->withTimestamps());die;
    //     return $this->belongsToMany(Project::class,ProjectUsers::class,'user_id','projectId')->withTimestamps();
    // }
    // public function getProjectIdAttribute($value)
    // {
    //     Log::info($value);
    //     // $res = Db::table('xfy_project')->where(['projectId'=>$value])->first();
    //     // print_r(explode(',', $value));die;
    //     // return $value;
    //     // return explode(',', $value);
    //     // return array_values(json_decode($res, true) ?: []);
    // }

    // public function setProjectIdAttribute($value)
    // {
    //     Log::info('aaa1');
    //     $this->attributes['projectId'] = json_encode(array_values($value));
    // }

    public function plans()
    {
        return $this->hasMany(ProjectPlan::class, 'project_id');
    }

}